﻿using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;
namespace DataAccess.DAL
{
   public  class MyProductDAL : BaseDAL
    {
       public MyProduct Get(int id)
        {
            return CONN_TopicSharing.Get<MyProduct>(id);
        }

       public bool Insert(MyProduct user)
        {
            return CONN_TopicSharing.Insert<MyProduct>(user);
        }

       public bool Update(MyProduct user)
        {
            return CONN_TopicSharing.Update<MyProduct>(user);
        }

        public bool Delete(int id)
        {
            string sql = " update [dbo].[MyProduct] set is_del=1 where id=@id  ";

            return CONN_TopicSharing.Excute(sql, new { id = id }) > 0;

        }

        public bool Insert(List<MyProduct> list)
        {
            //string sql = "INSERT INTO Product(Id,Name,Price) VALUES(newid(),@p,@d)";


            MyProduct model = list.FirstOrDefault();
                var ps = model.GetType().GetProperties();
                List<string> @colms = new List<string>();
                List<string> @params = new List<string>();

                foreach (var p in ps)
                {
                    if (!p.CustomAttributes.Any(x => x.AttributeType == typeof(XYD.Framework.KeyAttribute)) )
                    {
                        @colms.Add(string.Format("[{0}]", p.Name));
                        @params.Add(string.Format("@{0}", p.Name));
                    }
                }
                var sql = string.Format("INSERT INTO [{0}] ({1}) VALUES({2})", typeof(MyProduct).Name, string.Join(", ", @colms), string.Join(", ", @params));

              
            return CONN_TopicSharing.Excute(sql, list) > 0;
            
           
             

        }
        public List<MyProduct> ListPage(string strWhere, string orderbys, int pagesize, int pageindex, object param, out int count)
        {
            List<MyProduct> list = new List<MyProduct>();
            if (string.IsNullOrEmpty(strWhere))
            {
                strWhere = " is_del=0";
            }
        
            if (string.IsNullOrEmpty(orderbys))
                orderbys = "id desc ";

            list = CONN_TopicSharing.SQLPager<MyProduct>("*", " MyProduct ", strWhere, "", "", orderbys, pagesize, pageindex, out count, param).ToList();

            return list;
        }
        public bool Delete_ids(string ids, string promoter_id)
        {
            string sql = " update [MyProduct] set is_del=1 where promoter_id=@promoter_id and id in ("+ids+")  ";
            
            return CONN_TopicSharing.Excute(sql, new {  promoter_id = promoter_id }) > 0;

        }


    }
}
